package com.wally.auth.controller;

import java.net.URLEncoder;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wally.auth.util.WxEnum;

@Controller
// @RequestMapping
public class LoginController {

	public static final Logger log = LoggerFactory.getLogger(LoginController.class);

	@GetMapping("/data")
	@ResponseBody
	public String data(ModelMap map) {
		String str = "测试数据";
		Random rnd = new Random();
		return str + "::::" + rnd.nextInt(100);
	}

	@GetMapping("/")
	public String indexHmtl(ModelMap map) {
		map.addAttribute("host", "http://blog.wallyspace.com");
		return "index";
	}

	@GetMapping("/wallyPage")
	public String index(ModelMap map) {
		map.addAttribute("host", "http://blog.wallyspace.com");
		return "index";
	}

	// TODO 第一步：用户同意授权，获取code
	@SuppressWarnings("deprecation")
	@GetMapping("/login")
	public void Login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String backUrl = "http://purplerattle.w3.luyouxia.net/callBack";
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + "appid=" + WxEnum.APP_ID.getName()
				+ "&redirect_uri=" + URLEncoder.encode(backUrl) + "&response_type=code" + "&scope=snsapi_userinfo"
				+ "&state=STATE#wechat_redirect";
		response.sendRedirect(url);
	}
}
